﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FeiHongWeb.Admin
{
    public partial class ProductType : System.Web.UI.Page
    {
        private AccessHelp accesshelp = new AccessHelp();   
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Session["username"] == null) || (this.Session["username"].ToString() == string.Empty))
            {
                base.Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                base.Response.Write("<a href='Login.aspx' target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                base.Response.End();
            }
            if (!IsPostBack)
            {
                base.Response.Write("<script>parent.window.document.getElementById( 'lbl_title').innerHTML='产品类别'; </script>");
                string strSql = "select count(*) from [ProductsType]";
                AspNetPager1.RecordCount = int.Parse(accesshelp.getds(strSql).Tables[0].Rows[0][0].ToString());
                BindRpt();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }

        private void BindRpt()
        {
            string strSql = "select * from [ProductsType]";
            DataTable dt = accesshelp.GetListDataSet(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, strSql);
            this.gv_type.DataSource = dt;
            this.gv_type.DataBind();
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (this.btn_Add.Text == "增加")
            {
                string str = "insert into [ProductsType](Name,EnName,Flag) values('" + this.txt_type.Text.Trim() + "','" + this.txt_type2.Text.Trim() + "'," + this.ddl_type.SelectedValue + ") ";
                if (this.accesshelp.Insert(str) > 0)
                {
                    BindRpt();
                    base.Response.Write("<script>alert('添加成功');window.location='ProductType.aspx'</script>");
                }
                else
                    base.Response.Write("<script>alert('添加失败');</script>");
            }
            else if (this.btn_Add.Text == "修改")
            {
                int id = int.Parse(btn_Add.CommandArgument.ToString());
                string sql = "update [ProductsType] set Name='" + this.txt_type.Text.Trim() + "',EnName='" + this.txt_type2.Text.Trim() + "', Flag=" + this.ddl_type.SelectedValue + " where Id=" + id + "";
                if (this.accesshelp.update(sql) > 0)
                {
                    BindRpt();
                    base.Response.Write("<script>alert('修改成功');window.location='ProductType.aspx'</script>");
                }
                else
                    base.Response.Write("<script>alert('修改失败');</script>");
            }
        }

        protected void btn_return_Click(object sender, EventArgs e)
        {
            this.btn_Add.Text = "增加";
            this.txt_type.Text = "";
            this.txt_type2.Text = "";
            this.ddl_type.SelectedValue = "1";
        }

        protected void gv_type_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "up")
            {
                this.btn_Add.Text = "修改";
                int id = int.Parse(e.CommandArgument.ToString());
                btn_Add.CommandArgument = id.ToString();
                string strSql = "select * from [ProductsType] where Id=" + id + "";
                DataTable dt = accesshelp.getds(strSql).Tables[0];
                if (dt != null && dt.Rows.Count != 0)
                {
                    this.txt_type.Text = dt.Rows[0]["Name"].ToString().Trim();
                    this.txt_type2.Text = dt.Rows[0]["EnName"].ToString().Trim();
                    this.ddl_type.SelectedValue = dt.Rows[0]["Flag"].ToString().Trim();
                }
            }
            else if (e.CommandName == "del")
            {
                string strSql = "delete from [ProductsType] where Id=" + int.Parse(e.CommandArgument.ToString().Trim()) + "";
                string strSql2 = "delete from [Products] where Type=" + int.Parse(e.CommandArgument.ToString().Trim()) + "";
                if (accesshelp.Delete(strSql) > 0 && accesshelp.Delete(strSql2) >= 0)
                { 
                    BindRpt();
                    base.Response.Write("<script>alert('删除成功！');window.location='ProductType.aspx'</script>");
                }
                else
                    base.Response.Write("<script>alert('删除失败！');</script>");
            }
        }
    }
}